package org.faesa.masterSlave.master;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

import org.faesa.masterSlave.common.DecodeText;
import org.faesa.masterSlave.common.SlaveId;
import org.faesa.masterSlave.common.SlaveRegistration;

public class SlaveRegistrationImpl extends UnicastRemoteObject implements SlaveRegistration {
	private static final long serialVersionUID = 8310450052374537572L;
	
	protected SlaveRegistrationImpl() throws RemoteException {
		super();
	}

	@Override
	public SlaveId register() throws RemoteException {
		try {
			SlaveEntryPoint ep = SlaveEntryPoint.register((DecodeText)LocateRegistry.getRegistry().lookup("decode"));
			System.out.println(ep.getId().toString() + " registered!");
			ep.executeTask();
			return ep.getId();
		} catch (NotBoundException e) {
			return null;
		}
	}

	@Override
	public void unregister(SlaveId id) throws RemoteException {
		SlaveEntryPoint.unregister(id);
		System.out.println(id.toString() + " unregistered!");
	}

	@Override
	public void keepAlive(SlaveId id) throws RemoteException {
		SlaveEntryPoint.keepAlive(id);
		System.out.println("keepAlive from " + id.toString());
	}
}
